﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System.Data;
using System.IO;
using System.Web.Services;

namespace STT.Admin.UserControls
{
    public partial class ucBangDiem : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDSMonHoc();
                LoadDDLKhoaHoc();
                LoadGiangVien();
            }
        }
        protected void gvDiemHV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvDiemHV.PageIndex = e.NewPageIndex;
            LoadDSHV();
        }
        private void LoadDDLLophoc()
        {
            Methods.LopHocByKhoaHoc(ref ddlLopHoc, ddlKhoaHoc.SelectedValue.ToString());
        }
        private void LoadDDLKhoaHoc()
        {
            Methods.KhoaHoc(ref ddlKhoaHoc);
            Methods.KhoaHoc(ref ddlkhoahoc2);
        }
        private void LoadDSHV()
        {

            DataTable dt = BTbTKB.TbTKB_SelectByTKBMaMHMaLop(ddlLopHoc.SelectedValue.ToString(), ddlMonHoc.SelectedValue.ToString());
            GvDiemHV.DataSource = dt;
            GvDiemHV.DataBind();
            if (dt.Rows.Count > 0)
            {
                pn_dsHV.Visible = true;
                pn_DSDiemHV.Visible = false;
                lblmonhoc.Text = dt.Rows[0]["TenMonHoc"].ToString();
                lbltenlop.Text = dt.Rows[0]["TenLop"].ToString();
                lblgiangvien.Text = dt.Rows[0]["TenGV"].ToString();
                lblMaGV.Text = dt.Rows[0]["MaGV"].ToString();
            }
            else
            {
                pn_DSDiemHV.Visible = false;
                pn_dsHV.Visible = false;
            }
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void ddlLopHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlLopHoc.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn lớp học...!");

                }
                else
                {
                    Methods.MonHocByMaLopTKBNamHoc(ref ddlMonHoc, ddlLopHoc.SelectedValue.ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void btnthemdiem_Click(object sender, EventArgs e)
        {
            try
            {
                if (ddlLopHoc.SelectedItem.ToString() == "---------Chọn---------" ||
                    ddlMonHoc.SelectedItem.ToString() == "---------Chọn---------" ||
                    ddlLoaiDiem.SelectedValue.ToString() == "0")
                {
                    ThongBao("Chưa chọn lớp học hoặc môn học hoặc loại điểm...!");
                }
                else
                {
                    pn_dsHV.Visible = true;
                    pn_DSMonHoc.Visible = false;
                    LoadDSHV();
                }
            }
            catch { }
        }
        protected void GvDiemHV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    TextBox txtdiemcc = (TextBox)e.Row.FindControl("txtdiemcc");
                    TextBox txtdiemkt = (TextBox)e.Row.FindControl("txtdiemkt");
                    TextBox txtdiemtl1 = (TextBox)e.Row.FindControl("txtDiemTL1");
                    //TextBox txtdiemtl2 = (TextBox)e.Row.FindControl("txtDiemTL2");
                    if (ddlLoaiDiem.SelectedValue.ToString() == "1")
                    {
                        txtdiemcc.Enabled = true;
                        txtdiemkt.Enabled = false;
                        txtdiemtl1.Enabled = false;
                        //txtdiemtl2.Enabled = false;
                    }
                    else if (ddlLoaiDiem.SelectedValue.ToString() == "2")
                    {
                        txtdiemcc.Enabled = false;
                        txtdiemkt.Enabled = true;
                        txtdiemtl1.Enabled = false;
                        //txtdiemtl2.Enabled = false;
                    }
                    else if (ddlLoaiDiem.SelectedValue.ToString() == "3")
                    {
                        txtdiemcc.Enabled = false;
                        txtdiemkt.Enabled = false;
                        txtdiemtl1.Enabled = true;
                        //txtdiemtl2.Enabled = false;
                    }
           
                }
            }
            catch
            {
            }
        }

        protected void btnluudiem_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in GvDiemHV.Rows)
                {
                    if (ddlLoaiDiem.SelectedValue.ToString() == "1")
                    {
                        TextBox txtdiemcc = (TextBox)row.FindControl("txtdiemcc");
                        Label MaMH = row.FindControl("lblMamh") as Label;
                        string Mahv = GvDiemHV.DataKeys[row.RowIndex].Value.ToString();
                        ETbBangDiem bd = new ETbBangDiem();
                        if (txtdiemcc.Text != string.Empty && double.Parse(txtdiemcc.Text) >= 0 
                            && double.Parse(txtdiemcc.Text) <= 10)
                        {
                            bd.DiemChuyenCan = double.Parse(txtdiemcc.Text);
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemCC(bd,"3");
                            LoadDSHV();
                            //ThongBao("Đã lưu thành công..!");
                            Session["action"] = 2;
                        }
                        else
                        {
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemCC2(bd,"3");
                            LoadDSHV();
                            Session["action"] = 2;
                            //ThongBao("Bạn đã lưu thành công..!");
                        }

                    }
                    else if (ddlLoaiDiem.SelectedValue.ToString() == "2")
                    {
                        TextBox txtdiemkt = (TextBox)row.FindControl("txtdiemkt");
                        Label MaMH = row.FindControl("lblMamh") as Label;
                        string Mahv = GvDiemHV.DataKeys[row.RowIndex].Value.ToString();
                        ETbBangDiem bd = new ETbBangDiem();
                        if (txtdiemkt.Text != string.Empty && double.Parse(txtdiemkt.Text) >= 0 && 
                            double.Parse(txtdiemkt.Text) <= 10)
                        {
                            bd.DiemKT = double.Parse(txtdiemkt.Text);
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemKT(bd,"3");
                            LoadDSHV();
                            Session["action"] = 2;
                            //ThongBao("Đã lưu thành công..!");
                        }
                        else
                        {
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemKT2(bd,"3");
                            LoadDSHV();
                            Session["action"] = 2;
                            //ThongBao("Đã lưu thành công..!");
                        }

                    }
                    else if (ddlLoaiDiem.SelectedValue.ToString() == "3")
                    {
                        TextBox txtdiemtl1 = (TextBox)row.FindControl("txtDiemTL1");
                        Label MaMH = row.FindControl("lblMamh") as Label;
                        string Mahv = GvDiemHV.DataKeys[row.RowIndex].Value.ToString();
                        ETbBangDiem bd = new ETbBangDiem();
                        if (txtdiemtl1.Text != string.Empty && double.Parse(txtdiemtl1.Text) >= 0 && 
                            double.Parse(txtdiemtl1.Text) <= 10)
                        {
                            bd.DiemTL1 = double.Parse(txtdiemtl1.Text);
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemThiL1(bd,"3");
                            LoadDSHV();
                            Session["action"] = 2;
                            //ThongBao("Đã lưu thành công..!");
                        }
                        else
                        {
                            //bd.DiemTL1 = 0;
                            bd.MaHV = Mahv;
                            bd.MaMonHoc = int.Parse(MaMH.Text);
                            BTbBangDiem.UpdateDiemThiL12(bd,"3");
                            LoadDSHV();
                            Session["action"] = 2;
                            //ThongBao("Đã lưu thành công..!");
                        }

                    }
                }
            }
            catch { }
        }
        private void LoadDSMonHoc()
        {
            try
            {
                gvDSMonHoc.DataSource = tb.GetDataTable2("TbMonHoc_SelectByAdmin");
                gvDSMonHoc.DataBind();
            }
            catch { }
        }
        protected void btSuaDiem_Click(object sender, EventArgs e)
        {

        }
        public void Export1(DataTable dt, string sheetName, string title)
        {
            //T?o các ð?i tý?ng Excel
            Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks oBooks;
            Microsoft.Office.Interop.Excel.Sheets oSheets;
            Microsoft.Office.Interop.Excel.Workbook oBook;
            Microsoft.Office.Interop.Excel.Worksheet oSheet;

            //T?o m?i m?t Excel WorkBook 
            oExcel.Visible = true;
            oExcel.DisplayAlerts = false;
            oExcel.Application.SheetsInNewWorkbook = 1;
            oBooks = oExcel.Workbooks;

            oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
            oSheets = oBook.Worksheets;
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
            oSheet.Name = sheetName;

            // T?o ph?n ð?u n?u mu?n
            Microsoft.Office.Interop.Excel.Range head = oSheet.get_Range("B1", "E1");
            head.MergeCells = true;
            head.Value2 = title;
            head.Font.Bold = true;
            head.Font.Name = "Tahoma";
            head.Font.Size = "18";
            head.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            // T?o tiêu ð? c?t 
            Microsoft.Office.Interop.Excel.Range cl1 = oSheet.get_Range("A3", "A3");
            cl1.Value2 = "Mã học viên";
            cl1.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl2 = oSheet.get_Range("B3", "B3");
            cl2.Value2 = "Họ và tên";
            cl2.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl3 = oSheet.get_Range("C3", "C3");
            cl3.Value2 = "Tên Môn học";
            cl3.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl4 = oSheet.get_Range("D3", "D3");
            cl4.Value2 = "Điểm thành phần";
            cl4.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl5 = oSheet.get_Range("E3", "E3");
            cl5.Value2 = "Điểm thi";
            cl5.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl6 = oSheet.get_Range("F3", "F3");
            cl6.Value2 = "Điểm tổng kết";
            cl6.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range rowHead = oSheet.get_Range("A3", "F3");
            rowHead.Font.Bold = true;
            // K? vi?n
            rowHead.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            // Thi?t l?p màu n?n
            rowHead.Interior.ColorIndex = 15;
            rowHead.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;


            // T?o m?ng ð?i tý?ng ð? lýu d? toàn b? d? li?u trong DataTable,
            // v? d? li?u ðý?c ðý?c gán vào các Cell trong Excel ph?i thông qua object thu?n.
            object[,] arr = new object[dt.Rows.Count, dt.Columns.Count];

            //Chuy?n d? li?u t? DataTable vào m?ng ð?i tý?ng
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                DataRow dr = dt.Rows[r];
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    arr[r, c] = dr[c];
                }
            }

            //Thi?t l?p vùng ði?n d? li?u
            int rowStart = 4;
            int columnStart = 1;

            int rowEnd = rowStart + dt.Rows.Count - 1;
            int columnEnd = dt.Columns.Count;

            // Ô b?t ð?u ði?n d? li?u
            Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowStart, columnStart];
            // Ô k?t thúc ði?n d? li?u
            Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnEnd];
            // L?y v? vùng ði?n d? li?u
            Microsoft.Office.Interop.Excel.Range range = oSheet.get_Range(c1, c2);

            //Ði?n d? li?u vào vùng ð? thi?t l?p
            range.Value2 = arr;

            // K? vi?n
            range.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            // Cãn gi?a c?t STT
            Microsoft.Office.Interop.Excel.Range c3 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnStart];
            Microsoft.Office.Interop.Excel.Range c4 = oSheet.get_Range(c1, c3);
            oSheet.get_Range(c3, c4).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

        }
        protected void btnexport_Click(object sender, EventArgs e)
        {
            try
            {
            Button lbt = sender as Button;
            foreach (GridViewRow row in gvDSMonHoc.Rows)
            {
                string MaMH = gvDSMonHoc.DataKeys[row.RowIndex].Value.ToString();
                Button lbtmh = row.FindControl("btnexport") as Button;
                Control ctrl = row.FindControl("btnexport") as Button;
                Label lblmalop = ((Label)row.FindControl("lblmalop"));
                Label Getlblgiangvien = ((Label)row.FindControl("LabelMaGV"));
                Label Getlbltenlop = ((Label)row.FindControl("lbltenlop"));
                if (ctrl != null)
                {
                    Button lbt1 = (Button)ctrl;
                    if (lbt.ClientID == lbt1.ClientID)
                    {
                        DataTable dt = BTbTKB.TbTKB_SelectByMaGVMaMHMaLopExport(Getlblgiangvien.Text, 
                            lblmalop.Text, MaMH);
                        Export1(dt, "Danh sach", "DANH SÁCH ĐIỂM HỌC VIÊN" + "\n " + "LỚP : " + lbltenlop.Text);
                    }
                }
            }
            }
            catch { }
        }

        protected void gvDSMonHoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDSMonHoc.PageIndex = e.NewPageIndex;
            LoadDSMonHoc();
        }
        protected void btntemh_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton lbt = sender as LinkButton;
                foreach (GridViewRow row in gvDSMonHoc.Rows)
                {
                    Label lblMamh = row.FindControl("lblmamh") as Label;
                    LinkButton lbtmh = row.FindControl("btntemh") as LinkButton;
                    Control ctrl = row.FindControl("btntemh") as LinkButton;
                    Label lblmalop = ((Label)row.FindControl("lblmalop"));
                    Label Getlblgiangvien = ((Label)row.FindControl("LabelMaGV"));
                    Label Gettengiangvien = ((Label)row.FindControl("lblgiangvien"));
                    Label Getlbltenlop = ((Label)row.FindControl("lbltenlop"));
                    if (ctrl != null)
                    {
                        LinkButton lbt1 = (LinkButton)ctrl;
                        if (lbt.ClientID == lbt1.ClientID)
                        {
                            Label5.Text = "Danh sách điểm học viên";
                            pn_DSMonHoc.Visible = false;
                            pn_DSDiemHV.Visible = true;
                            DataTable dt = BTbTKB.TbTKB_SelectByTKBMaMHMaLop(lblmalop.Text, lblMamh.Text);
                            lblmonhoc2.Text = lbtmh.Text;
                            lbltenlop2.Text = Getlbltenlop.Text;
                            lblgiangvien2.Text = Gettengiangvien.Text;
                            lblMaGV2.Text = Getlblgiangvien.Text;
                            lblMaMH2.Text = lblMamh.Text;
                            lblMaLop2.Text = lblmalop.Text;
                            if (dt.Rows.Count > 0)
                            {
                                btnexportCT.Visible = true; 
                                Pn_DiemHVCT.Visible = true;
                                DivHeadHVCT.Visible = true;
                                gv_dsDiemHV.DataSource = dt;
                                gv_dsDiemHV.DataBind();
                                
                            }
                            else {

                                DivHeadHVCT.Visible = false;
                                btnexportCT.Visible = false;  
                            }
                        }
                    }
                }
            }
            catch { }
        }

        protected void ddlKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlKhoaHoc.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn khóa học...!");

                }
                else
                {
                    LoadDDLLophoc();
                }
            }
            catch { }
        }

        protected void ddlKhoahoc2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlkhoahoc2.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn khóa học...!");

                }
                else
                {
                    Methods.LopHocByKhoaHoc(ref ddllophoc2, ddlkhoahoc2.SelectedValue.ToString());
                }
            }
            catch { }
        }

        protected void ddllophoc2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddllophoc2.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn lớp học...!");

                }
                else
                {
                    Methods.MonHocByMaLopTKBNamHoc(ref ddlmonhoc2, ddllophoc2.SelectedValue.ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }
        private void LoadGiangVien()
        {
            Methods.GiangVienSelectAll(ref ddlGiangvien);
        }
        protected void ddlGiangvien_SelectedIndexChanged(object sender, EventArgs e)
        {
           try
           {
               if (ddlGiangvien.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn giảng viên...!");

                }
            else
                {
                    Label5.Text = "Danh sách các môn học của các lớp";
                    gv_dsDiemHV.DataSource = null;
                    gv_dsDiemHV.DataBind();
                    pn_DSDiemHV.Visible = false;
                    pn_DSMonHoc.Visible = true; 
                    gvDSMonHoc.DataSource = tb.GetDataTableById2("TbTKB_SelectByMaGV", ddlGiangvien.SelectedValue.ToString());
                    gvDSMonHoc.DataBind();   
                }
              }
             catch { }
        }

        protected void btnxemdsmonhoc_Click(object sender, EventArgs e)
        {
            Label5.Text = "Danh sách điểm học viên";
            pn_DSMonHoc.Visible = false;
            pn_DSDiemHV.Visible = true;
            DataTable dt = BTbTKB.TbTKB_SelectByTKBMaMHMaLop(ddllophoc2.SelectedValue.ToString(), ddlmonhoc2.SelectedValue.ToString());
             if (dt.Rows.Count > 0)
                {
                    btnexportCT.Visible = true; 
                    Pn_DiemHVCT.Visible = true;
                    lblgiangvien2.Text = dt.Rows[0]["TenGV"].ToString();
                    lblMaGV2.Text = dt.Rows[0]["MaGV"].ToString();
                    lblmonhoc2.Text = dt.Rows[0]["TenMonHoc"].ToString();
                    lblMaMH2.Text = dt.Rows[0]["MaMonHoc"].ToString();
                    lblMaLop2.Text = dt.Rows[0]["MaLop"].ToString();
                    lbltenlop2.Text = dt.Rows[0]["TenLop"].ToString();
                    gv_dsDiemHV.DataSource = dt;
                    gv_dsDiemHV.DataBind();
                }
                 else
                 {
                     lblgiangvien2.Text = "";
                     lblMaGV2.Text = "";
                     lblmonhoc2.Text = "";
                     lblMaMH2.Text = "";
                     lblMaLop2.Text = "";
                     lbltenlop2.Text = "";
                     Pn_DiemHVCT.Visible = false;
                     btnexportCT.Visible = false;
                 }
        }

        protected void ddlmonhoc2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            Label5.Text = "Danh sách các môn học của các lớp";
            gv_dsDiemHV.DataSource = null;
            gv_dsDiemHV.DataBind();
            pn_DSDiemHV.Visible = false;
            pn_DSMonHoc.Visible = true; 
        }

        protected void btnBackEdit_Click(object sender, EventArgs e)
        {            
            pnbangdiem.Visible = true;
            pnDSMonHoc.Visible = false;          
        }

        protected void btnxemdsdiem_Click(object sender, EventArgs e)
        {
            pnbangdiem.Visible = false;
            pnDSMonHoc.Visible = true;
            LoadDSMonHoc();
        }

        protected void btnexportCT_Click(object sender, EventArgs e)
        {
            if (ddlkhoahoc2.SelectedItem.ToString() == "---------Chọn---------" ||
                ddllophoc2.SelectedItem.ToString() == "---------Chọn---------" ||
                ddlmonhoc2.SelectedItem.ToString() == "---------Chọn---------")
            {
                DataTable dt = BTbTKB.TbTKB_SelectByMaGVMaMHMaLopExport(lblMaGV2.Text, lblMaLop2.Text, lblMaMH2.Text);
                Export1(dt, "Danh sach", "DANH SÁCH ĐIỂM HỌC VIÊN" + "\n " + "LỚP : " + lbltenlop.Text);
               
            }
            else
            {

                //ThongBao(ddllophoc2.SelectedValue.ToString()+" " + ddlmonhoc2.SelectedValue.ToString());
                DataTable dt = BTbTKB.TbTKB_SelectByMaGVMaMHMaLopExport(lblMaGV2.Text, lblMaLop2.Text, lblMaMH2.Text);
                Export1(dt, "Danh sach", "DANH SÁCH ĐIỂM HỌC VIÊN" + "\n " + "LỚP : " + lbltenlop.Text);
            }
        }
        [WebMethod]
        public static void LoadDDLLophoc(DropDownList ddlLopHoc, DropDownList ddlKhoaHoc)
        {
            Methods.LopHocByKhoaHoc(ref ddlLopHoc, ddlKhoaHoc.SelectedValue.ToString());
        }
  }
}
    


      

      
        
        
        




       
       

      

    

            
 